const db = require('../config/db')

class User {

    // 是否注册账号(微信)
    iswxregister(openid){
        return new Promise((resolve,reject)=>{
             db.query(`select * from db_user where openid = '${openid}'`)
             .then(
                 rows=>{
                     if(rows.length === 0) {
                        resolve({
                            code: -1,
                            msg: "您还没注册账户。",
                         })
                     }
                     else {
                        resolve({
                            code: 200,
                            msg: "该微信号已被注册。",
                            user_phone:rows[0].user_phone
                        })
                     }
                 },
                 err=>{
                     reject({
                        code: 500,
                        msg:"查询账号信息失败。",
                        err 
                     })
                 }
             )
             
        })
    }


    // 注册账号(微信)
    wxregister(user_name,user_email,user_phone,user_pwd,user_create_time,openid,user_imgUrl){
        return new Promise((resolve,reject)=>{
             db.query(`select * from db_user where openid = '${openid}'`)
             .then(
                 rows=>{
                     if(rows.length === 0) {
                        let sql = `insert into db_user(user_name,user_email,user_phone,user_pwd,user_create_time,openid,user_imgUrl) 
                        values('${user_name}','${user_email}','${user_phone}','${user_pwd}','${user_create_time}','${openid}','${user_imgUrl}')`;
                        db.query(sql)
                        .then(
                             content=>{
                                 resolve({
                                    code: 200,
                                    msg: "微信端注册成功。",
                                    user_email,
                                    user_phone,
                                    content
                                 })
                             },
                             err=>{
                                 reject({
                                    code: 500,
                                    msg: "微信端注册失败。",
                                    err
                                 })
                             }
                        )
                     }
                     else {
                        resolve({
                            code: 1,
                            msg: "该微信号已被注册。",
                            user_email
                        })
                     }
                 },
                 err=>{
                     reject({
                        code: 500,
                        msg:"查询账号信息失败。",
                        err 
                     })
                 }
             )
             
        })
    }

    // 登录账号(微信)
    wxlogin(user_phone){
        return new Promise((resolve,reject)=>{
            db.query(`select * from db_user where user_phone = '${user_phone}'`)
            .then(
                rows=>{
                    if(rows && rows.length === 1) {
                        // 把用户密码记录着 [{user_pwd:'xxxxxadfadfadsadfa'}]
                        resolve(rows[0])
                    }
                    else {
                        resolve({
                            cod: 404,
                            msg: "该账号不存在",
                            rows 
                        })
                    }
                },
                err=> {
                    reject({
                        code: 500,
                        err,
                        msg: "服务端异常。"
                    })
                }
            )
        })
    }

    // 获取用户信息（微信）
    wxinfo(id,user_phone){
        return new Promise((resolve,reject)=>{
            db.query(`select user_name,user_email,user_phone,user_create_time from db_user where id = '${id}' and user_email = '${user_email}'`)
            .then(
                rows=>{
                     if(rows.length === 1) {
                         resolve({
                            code: 200,
                            msg: "获取用户信息成功。",
                            data: rows[0]
                         })
                     }
                     else {
                        resolve({
                            code: 404,
                            msg: "该账号不存在。",
                            rows
                        })
                     }
                },
                err=> {
                    reject({
                        code: 500,
                        msg: "服务端发生异常。"
                    })
                }
            )
        })
    }

    // 注册账号
    register(user_name,user_email,user_phone,user_pwd,user_create_time){
        return new Promise((resolve,reject)=>{
             db.query(`select user_email from db_user where user_email = '${user_email}'`)
             .then(
                 rows=>{
                     if(rows.length === 0) {
                        let sql = `insert into db_user(user_name,user_email,user_phone,user_pwd,user_create_time) 
                        values('${user_name}','${user_email}','${user_phone}','${user_pwd}','${user_create_time}')`;
                        db.query(sql)
                        .then(
                             content=>{
                                 resolve({
                                    code: 200,
                                    msg: "注册成功。",
                                    user_email,
                                    content
                                 })
                             },
                             err=>{
                                 reject({
                                    code: 500,
                                    msg: "注册失败。",
                                    err
                                 })
                             }
                        )
                     }
                     else {
                        resolve({
                            code: 1,
                            msg: "账号已经存在。",
                            user_email
                        })
                     }
                 },
                 err=>{
                     reject({
                        code: 500,
                        msg:"查询账号信息失败。",
                        err 
                     })
                 }
             )
             
        })
    }

    // 登录账号
    login(user_email){
        return new Promise((resolve,reject)=>{
            db.query(`select * from db_user where user_email = '${user_email}'`)
            .then(
                rows=>{
                    if(rows && rows.length === 1) {
                        // 把用户密码记录着 [{user_pwd:'xxxxxadfadfadsadfa'}]
                        resolve(rows[0])
                    }
                    else {
                        resolve({
                            cod: 404,
                            msg: "该账号不存在",
                            rows 
                        })
                    }
                },
                err=> {
                    reject({
                        code: 500,
                        err,
                        msg: "服务端异常。"
                    })
                }
            )
        })
    }

    // 获取用户信息
    info(id){
        return new Promise((resolve,reject)=>{
            db.query(`select user_name,user_email,user_phone,user_create_time,user_addr,user_birthday,user_intro,user_sex from db_user where id = '${id}'`)
            .then(
                rows=>{
                     if(rows.length === 1) {
                         resolve({
                            code: 200,
                            msg: "获取用户信息成功。",
                            data: rows[0]
                         })
                     }
                     else {
                        resolve({
                            code: 404,
                            msg: "该账号不存在。",
                            rows
                        })
                     }
                },
                err=> {
                    reject({
                        code: 500,
                        msg: "服务端发生异常。"
                    })
                }
            )
        })
    }
    // 修改资料
    // user_addr,user_birthday,user_intro,user_sex
    modify(id,user_addr,user_birthday,user_intro,user_sex){
        return new Promise((resolve,reject)=>{
            db.query(`select * from db_user where id = '${id}'`)
            .then(
                rows=>{
                     if(rows.length === 1) {
                        let sql = `update db_user set user_addr= '${user_addr}',user_birthday= '${user_birthday}',user_intro= '${user_intro}',user_sex= '${user_sex}' where  id= '${id}'`;
                        db.query(sql)
                        .then(
                            () => {
                                resolve({
                                    code: 200,
                                    msg: '设置资料成功。'
                                })
                            },
                            () => {
                                resolve({
                                    code: 200,
                                    msg: '设置资料失败。'
                                })
                            }
                        )
                     }
                     else {
                        resolve({
                            code: 404,
                            msg: "该账号不存在。",
                            rows
                        })
                     }
                },
                err=> {
                    reject({
                        code: 500,
                        msg: "服务端发生异常。"
                    })
                }
            )
        })
    }

    // 删除账号
    remove(id) {
        return new Promise((resolve, reject) => {
            db.query(`select * from db_user where id = '${id}'`)
                .then(
                    rows => {
                        if (rows && rows.length === 1) {
                            // 存在
                            let sql = `delete from db_user where id = '${id}'`
                            db.query(sql)
                                .then(
                                    content => {
                                        resolve({
                                            code: 200,
                                            msg: "删除账号成功",
                                            content
                                        })
                                    },
                                    err => {
                                        reject({
                                            code: 500,
                                            msg: "删除账号失败",
                                            err
                                        })
                                    }
                                )
                        }
                        else {
                            // 不存在
                            resolve({
                                code: 404,
                                msg: "不存在这个账号"
                            })
                        }
                    },
                    err => {
                        reject({
                            code: 500,
                            msg: "查询账号信息失败",
                            err
                        })
                    }
                )
        })
    }

     // 获取头像
     getUserimgUrl(id) {
        return new Promise((resolve, reject) => {
            db.query(`select * from db_user where id = '${id}'`)
                .then(
                    rows => {
                        if (rows && rows.length === 1) {
                            // 存在
                            resolve({
                                code: 200,
                                msg: "获取信息成功",
                                result: {
                                    user_name: rows[0].user_name,
                                    user_imgUrl: rows[0].user_imgUrl,
                                    user_phone: rows[0].user_phone
                                }
                            })
                        }
                        else {
                            // 不存在
                            resolve({
                                code: 404,
                                msg: "不存在这个账号"
                            })
                        }
                    },
                    err => {
                        reject({
                            code: 500,
                            msg: "查询账号信息失败",
                            err
                        })
                    }
                )
        })
    }



      // 修改用户名
      modify_name(user_id, user_name) {
        return new Promise((resolve, reject) => {
            let sql = `select user_id from db_user where user_id = '${user_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 1) {
                            // 修改用户资料
                            let sql2 = `update db_user set user_name= '${user_name}' where  user_id= '${user_id}'`;

                            db.query(sql2)
                                .then(
                                    (result) => {
                                        // 判断是否修改成功
                                        if (result.affectedRows == 1) {
                                            resolve({
                                                code: 200,
                                                msg: '修改用户名成功。'
                                            })
                                        }
                                        else {
                                            resolve({
                                                code: -1,
                                                msg: '修改用户名失败。'
                                            })
                                        }

                                    },
                                    (err) => {
                                        reject({
                                            code: -1,
                                            msg: '服务端发生异常。222',
                                            err
                                        })
                                    }
                                )
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '该用户不存在。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '服务端发生异常。111',
                            err
                        })
                    }
                )
        })
    }
    // 示例
    foo(){
        return new Promise((resolve,reject)=>{

        })
    }
}


module.exports = new User();